package com.ccys.cms.service.thinkWell.impl;


import cn.hutool.core.util.IdUtil;
import com.ccys.cms.service.thinkWell.TblThinkWellContentWebService;
import com.ccys.common.util.StringUtil;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.cache.UserUtil;
import com.ccys.db.dto.thinkWell.TblThinkWellContentDTO;
import com.ccys.db.entity.TblThinkWellContent;
import com.ccys.db.entity.TblThinkWellContentCategory;
import com.ccys.db.entity.TblThinkWellContentSubCategory;
import com.ccys.db.mapper.TblThinkWellContentCategoryMapper;
import com.ccys.db.mapper.TblThinkWellContentMapper;
import com.ccys.db.mapper.TblThinkWellContentSubCategoryMapper;
import com.ccys.db.page.PageUtils;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.service.BaseTblAuditTrailWebService;
import com.ccys.db.vo.thinkWell.TblThinkWellContentWebVO;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;


/**
 * tbl_think_well_content管理
 */
@Service
@Slf4j
public class TblThinkWellContentWebServiceImpl extends BaseServiceImpl<TblThinkWellContent> implements TblThinkWellContentWebService {

    @Autowired(required = false)
    private TblThinkWellContentMapper tblThinkWellContentMapper;

    @Autowired(required = false)
    private TblThinkWellContentCategoryMapper tblThinkWellContentCategoryMapper;
    @Autowired(required = false)
    private TblThinkWellContentSubCategoryMapper tblThinkWellContentSubCategoryMapper;
    @Autowired(required = false)
    private BaseTblAuditTrailWebService baseTblAuditTrailWebService;

    @Override
    public ResponseResult listObjectsCms(HttpServletRequest request, HttpServletResponse response, TblThinkWellContentDTO dto) {
        PageUtils.startPage(request);
        List<TblThinkWellContentWebVO> list = this.tblThinkWellContentMapper.listObjectsCms(dto);
        PageInfo pageInfo = new PageInfo<>(list);
        return ResponseResult.success(pageInfo);
    }


    @Override
    public ResponseResult change(TblThinkWellContentDTO entity) {
        String categoryIds = entity.getCategoryIds();
        List<TblThinkWellContentSubCategory> list = entity.getTblThinkWellContentSubCategoryList();
        if(list != null) {
            if(StringUtil.isEmpty(list.get(0).getCategoryId())) {
                return ResponseResult.error("所属一级分类ID必出");
            }

        }
        if (StringUtils.isBlank(entity.getId())) {
            if(StringUtil.isNotEmpty(categoryIds)) {
                return ResponseResult.error("categoryIds is must");
            }

            // 新增
            this.saveEntity(entity);
            String contentId = entity.getId();
            saveContentCategoryRelation(categoryIds, contentId);
            saveContentCategorySubRelation(list, contentId);
        } else {
            // 编辑
            this.updateEntityById(entity);
            if(entity.getStatus() != null) {
                this.baseTblAuditTrailWebService.saveStatusLog(entity,entity.getStatus());
            }
            List<String> whereList = new ArrayList<>();
            whereList.clear();
            whereList.add("del_flag=0");
            whereList.add("content_id='"+entity.getId()+"'");
            TblThinkWellContentCategory contentCategoryUpdate = new TblThinkWellContentCategory();
            contentCategoryUpdate.setDelFlag(0);
            contentCategoryUpdate.setContentId(entity.getId());
            if(StringUtil.isNotEmpty(categoryIds)) {
                tblThinkWellContentCategoryMapper.delete(contentCategoryUpdate);
                saveContentCategoryRelation(categoryIds, entity.getId());
            }
            if(list != null && !list.isEmpty()) {
                TblThinkWellContentSubCategory contentSubCategoryUpdate = new TblThinkWellContentSubCategory();
                contentSubCategoryUpdate.setDelFlag(0);
                contentSubCategoryUpdate.setContentId(entity.getId());
                tblThinkWellContentSubCategoryMapper.delete(contentSubCategoryUpdate);
                saveContentCategorySubRelation(list, entity.getId());
            }
        }
        return ResponseResult.success();
    }

       /**
        * @description 保存内容与一级分类关系
        * @date     2023/12/18
        * @author   cloud fly
        */
    private void saveContentCategoryRelation(String categoryIds, String contentId) {
        List<TblThinkWellContentCategory> contentCategoryList = new ArrayList<>(categoryIds.split(",").length);
        try {
            for (String s : categoryIds.split(",")) {
                TblThinkWellContentCategory contentCategory = new TblThinkWellContentCategory();
                contentCategory.setContentId(contentId);
                contentCategory.setCategoryId(s);
                contentCategory.setCreatedOnUtc(System.currentTimeMillis());
                contentCategory.setCreatedBy(UserUtil.getCurLoginUserId());
                contentCategory.setId(IdUtil.randomUUID());
                contentCategory.setDelFlag(0);
                contentCategoryList.add(contentCategory);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(!contentCategoryList.isEmpty()) {
            this.tblThinkWellContentCategoryMapper.batchInsert(contentCategoryList);
        }
    }

       /**
        * @description 保存内容与二级级分类关系
        * @date     2023/12/18
        * @author   cloud fly
        */
    private void saveContentCategorySubRelation(List<TblThinkWellContentSubCategory> list, String contentId) {
        if(list == null || list.isEmpty()) {
            return;
        }
        List<TblThinkWellContentSubCategory> contentCategoryList = new ArrayList<>(list.size());
        try {
            for (TblThinkWellContentSubCategory contentCategory : list) {
                contentCategory.setContentId(contentId);
                contentCategory.setCreatedOnUtc(System.currentTimeMillis());
                contentCategory.setCreatedBy(UserUtil.getCurLoginUserId());
                contentCategory.setId(IdUtil.randomUUID());
                contentCategory.setDelFlag(0);
                contentCategoryList.add(contentCategory);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(!contentCategoryList.isEmpty()) {
            this.tblThinkWellContentSubCategoryMapper.batchInsert(contentCategoryList);
        }
    }

    @Override
    public ResponseResult delete(TblThinkWellContent dto) {
        TblThinkWellContent entity = new TblThinkWellContent();
        entity.setId(dto.getId());
        entity.setDelFlag(1);
        int i = this.updateEntityById(entity);
        if(i > 0) {
            TblThinkWellContentCategory contentCategoryUpdate = new TblThinkWellContentCategory();
            contentCategoryUpdate.setDelFlag(0);
            contentCategoryUpdate.setContentId(entity.getId());
            tblThinkWellContentCategoryMapper.delete(contentCategoryUpdate);
            TblThinkWellContentSubCategory contentSubCategoryUpdate = new TblThinkWellContentSubCategory();
            contentSubCategoryUpdate.setDelFlag(0);
            contentSubCategoryUpdate.setContentId(entity.getId());
            tblThinkWellContentSubCategoryMapper.delete(contentSubCategoryUpdate);
        }


        return ResponseResult.success();
    }


}